package com.liaoban.app.ui.anchor;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.liaoban.app.R;
import com.liaoban.app.api.type.Report;
import com.liaoban.app.api.type.request.AddReport;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.constants.Constant;
import com.liaoban.app.constants.ExtraParam;
import com.liaoban.app.helper.ImageHelper;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.base.BaseUploadPicActivity;
import com.liaoban.app.widget.MyGridLayoutManager;
import com.liaoban.app.widget.MyTextWatcher;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ybc on 2018/7/30 0030.
 * 举报
 */
public class ReportActivity extends BaseUploadPicActivity {
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.tv_report)
    TextView tvReport;
    @BindView(R.id.et_description)
    EditText etDescription;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.iv_img)
    ImageView ivImg;

    private ReportAdapter mAdapter;
    private File mFile; //图片
    private String mWuserId;

    public static Intent newIntent(Context context, String wuserId) {
        Intent intent = new Intent(context, ReportActivity.class);
        intent.putExtra(ExtraParam.ID, wuserId);
        return intent;
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_report;
    }

    @Override
    protected String getNewTitle() {
        return "举报";
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mWuserId = getIntent().getStringExtra(ExtraParam.ID);
        initViewList();
        initListener();
        loadData();
    }

    private void initViewList() {
        MyGridLayoutManager manager = new MyGridLayoutManager(this, 1);
        rvData.setLayoutManager(manager);
        mAdapter = new ReportAdapter(null);
        rvData.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this::onItemClick);
    }

    private void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        mAdapter.setSel(position);
        mAdapter.notifyDataSetChanged();
    }

    private void initListener() {
        MyTextWatcher myTextWatcher = new MyTextWatcher(() -> {
            int count = etDescription.getText().toString().trim().length();
            tvCount.setText(count + "/500");
            changeReportListener();
        });
        etDescription.addTextChangedListener(myTextWatcher);
    }

    private void changeReportListener() {
        if (!TextUtils.isEmpty(etDescription.getText().toString()) && mFile != null) {
            tvReport.setEnabled(true);
            tvReport.setBackgroundResource(R.color.btn_yellow);
        } else {
            tvReport.setEnabled(false);
            tvReport.setBackgroundResource(R.color.report_color_default);
        }
    }

    private void loadData() {
        showRunningDialog();
        startTask(CommonBiz.getInstance().reportList(), listBaseResponse -> {
            mAdapter.setNewData(listBaseResponse.data);
        });
    }

    @OnClick(R.id.iv_img)
    public void img() {
        showSheetDialog(false);
    }

    @OnClick(R.id.tv_report)
    public void report() {
        showRunningDialog(R.string.committing);
        AddReport addReport = new AddReport();
        addReport.setWuserId(mWuserId);
        addReport.setDescription(etDescription.getText().toString().trim());
        Report report = mAdapter.getData().get(mAdapter.getSel());
        addReport.setType(report.getId() + "");
        startTask(CommonBiz.getInstance().addReport(addReport, mFile), baseResponse -> {
            ToastHelper.show(this, baseResponse.msg);
            finish();
        });
    }

    @Override
    protected void uploadPicSuccessful(boolean isTailor, File outFile, Uri filePath) {
        ImageHelper.load(ivImg, String.valueOf(outFile));
        mFile = outFile;
        changeReportListener();
    }
}
